home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: why get 9 strings only?
- Date: Wed, 24 Jan 96 17:25:37 GMT
- Organization: none
- Message-ID: <822504337snz@genesis.demon.co.uk>
- References: <4e51th$4bi@news.nevada.edu> <4e5km4$2e6@news.iag.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4e5km4$2e6@news.iag.net> jatmon@iag.net "John R Buchan" writes:
-
- >In article <4e51th$4bi@news.nevada.edu>, chancl@nevada.edu says...
- >>
- >>I can't figure out why I can only input 9 strings
- >>instead of 10 on the following program:
-
- >>{
- >> char str[10][80];
- >> int i, j;
- >>
- >> printf("Enter a number (0-9):\n");
- >> scanf("%d", &j);
- >
- >Using scanf for input to input numeric values is tricky. Here you've
- >told it to read an integer value and store it in j. It obediently
- >reads chars until it reaches the first one that isn't allowed for ints.
- >In this case probably a '\n'. It leaves the unused char in the buffer,
- >converts the read chars, and stores the value in j.
-
- That's one problem.
-
- ...
-
- >>
- >> if(j>0 && j<10)
- >> printf("Answer: %s\n", str[j]);
- >>}
-
- This only allows you to select indices between 1 and 9 i.e. 9 values. To
- see the first one you need to make the first test j>=0. Couples with the
- first problem this makes it look like it reads only the first 9 lines
- and indexes the first line from 1 rather than 0.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-